package com.example.biji;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;

public class CRUD {
    private SQLiteOpenHelper dbHandler;
    private SQLiteDatabase db;

    private static final String[] columns = {
            NoteDatabase.ID,
            NoteDatabase.CONTENT,
            NoteDatabase.TIME,
            NoteDatabase.MODE,
            NoteDatabase.PATH,
            NoteDatabase.FLAG,
            NoteDatabase.LOCATION
    };

    public CRUD(Context context) {
        dbHandler = new NoteDatabase(context);
    }/*向上造型*/

    public void open(){
        db = dbHandler.getWritableDatabase();
    }

    public void close(){
        dbHandler.close();
    }

    //把note 加入到database里面,并返回添加到表中的note*/
    public Note addNote(Note note){
        //add a note object to database
        ContentValues contentValues = new ContentValues();
        contentValues.put(NoteDatabase.CONTENT, note.getContent());
        contentValues.put(NoteDatabase.TIME, note.getTime());
        contentValues.put(NoteDatabase.MODE, note.getTag());
        contentValues.put(NoteDatabase.FLAG, note.getFlag());
        contentValues.put(NoteDatabase.LOCATION, note.getLocation());
        contentValues.put(NoteDatabase.PATH, note.getPath() + "");

        long insertId = db.insert(NoteDatabase.TABLE_NAME, null, contentValues);
        /*将note加入到表中,insert返回一个增长的id（创建表时设立)*/
        note.setId(insertId);/*再设置note的id*/
        return note;
    }

    public List<Note> getAllNotes(){
        Cursor cursor = db.query(NoteDatabase.TABLE_NAME, columns, null, null, null, null, null);
        /*查询，得到全部记录*/
        List<Note> notes = new ArrayList<>();
        if (cursor.getCount() > 0){
            while (cursor.moveToNext()){
                Note note = new Note();
                note.setId(cursor.getLong(cursor.getColumnIndex(NoteDatabase.ID)));
                note.setContent(cursor.getString(cursor.getColumnIndex(NoteDatabase.CONTENT)));
                note.setTime(cursor.getString(cursor.getColumnIndex(NoteDatabase.TIME)));
                note.setTag(cursor.getInt(cursor.getColumnIndex(NoteDatabase.MODE)));
                note.setPath(cursor.getString(cursor.getColumnIndex(NoteDatabase.PATH)));
                note.setFlag(cursor.getInt(cursor.getColumnIndex(NoteDatabase.FLAG)));
                note.setLocation(cursor.getString(cursor.getColumnIndex(NoteDatabase.LOCATION)));

                notes.add(note);
                /*一步一步移动记录指针，把所有记录都加到notes上,指针指向一行记录，每个值需要索引(是第几列)得到，*/
            }
        }
        cursor.close();
        return notes;
    }

    public int updateNote(Note note) {
        //update the info of an existing note
        ContentValues values = new ContentValues();
        values.put(NoteDatabase.CONTENT, note.getContent());
        values.put(NoteDatabase.TIME, note.getTime());
        values.put(NoteDatabase.MODE, note.getTag());
        values.put(NoteDatabase.PATH, note.getPath());
        values.put(NoteDatabase.FLAG, note.getFlag());
        values.put(NoteDatabase.LOCATION, note.getLocation());

        //updating row
        return db.update(NoteDatabase.TABLE_NAME, values,
                NoteDatabase.ID + "=" + note.getId(),null);
        /*sql，更新语句,更新一行,这样是根据Id值是一样的*/
        /*这里 whereClause中的=？,后面的参数是whereArgs，是字符数组，将一一替换?*/
    }

    public void removeNote(Note note) {
        //remove a note according to ID value
        db.delete(NoteDatabase.TABLE_NAME, NoteDatabase.ID + "=" + note.getId(), null);
    }/*注意这个是= */

}
